package com.example.administrator.androidros.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.androidros.R;
import com.example.administrator.androidros.activity.SceneDetailsActivity;
import com.hhc.netty.DeviceModel;

import java.util.List;

/**
 * Created by huhaichao on 2018/3/26.
 */

public class SceneAdapter extends RecyclerView.Adapter<SceneAdapter.ViewHolder> {
    private Context mContext;
    private List<DeviceModel> data;
    private OnItemClickListener onItemClickListener;

    public SceneAdapter(Context mContext, List<DeviceModel> data) {
        this.mContext = mContext;
        this.data = data;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.activity_scene_item_scene, parent, false);
        ViewHolder mViewHolder = new ViewHolder(view);
        return mViewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (data.get(position).getSceneType().equals("10")) {
            holder.activity_scene_item_scene_img_icon.setImageResource(R.mipmap.ic_launcher_round);
        } else if (data.get(position).getSceneType().equals("20")) {
            holder.activity_scene_item_scene_img_icon.setImageResource(R.mipmap.ic_launcher_round);
        } else if (data.get(position).getSceneType().equals("30")) {
            holder.activity_scene_item_scene_img_icon.setImageResource(R.mipmap.ic_launcher_round);
        }
        holder.activity_scene_item_scene_tv_name.setText(data.get(position).getSceneName());

        holder.activity_scene_item_scene_tv_number.setText(mContext.getString(R.string.scene_appliance_number,data.get(position).getNumber()));
        Spannable span = new SpannableString( holder.activity_scene_item_scene_tv_number.getText().toString());//获取按钮的文字
        span.setSpan(new ForegroundColorSpan(Color.RED), 3,6 , Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        holder.activity_scene_item_scene_tv_number.setAllCaps(false);
        holder.activity_scene_item_scene_tv_number.setText(span);

        holder.activity_scene_item_scene_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                onItemClickListener.onClick();
                Bundle bundle = new Bundle();
                bundle.putString("sceneMasterId", data.get(position).getMasterId());
                bundle.putString("title", data.get(position).getSceneName());
                Intent intent = new Intent();
                intent.setClass(mContext, SceneDetailsActivity.class);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView activity_scene_item_scene_img_icon;
        private TextView activity_scene_item_scene_tv_name;
        private TextView activity_scene_item_scene_tv_number;
        private RelativeLayout activity_scene_item_scene_item;

        public ViewHolder(View itemView) {
            super(itemView);
            activity_scene_item_scene_img_icon = (ImageView) itemView.findViewById(R.id.activity_scene_item_scene_img_icon);
            activity_scene_item_scene_tv_name = (TextView) itemView.findViewById(R.id.activity_scene_item_scene_tv_name);
            activity_scene_item_scene_tv_number = (TextView) itemView.findViewById(R.id.activity_scene_item_scene_tv_status);
            activity_scene_item_scene_item = (RelativeLayout) itemView.findViewById(R.id.activity_scene_item_scene_item);
        }
    }

    public void setNewData(List<DeviceModel> list) {
        data.clear();
        data.addAll(list);
        notifyDataSetChanged();
    }

    public interface OnItemClickListener {
        void onClick();
    }
}
